<?php

namespace app\enterprise\controller;

use app\adminapi\controller\AuthController;
use think\exception\ValidateException;

use app\enterprise\model\Project as projectModel;
use app\enterprise\model\Parameter as parameterModel;

/**
 * 项目相关
 */
class Project extends AuthController
{
    public function page()
    {
        $param = $this->request->param();

        $where[] = ['company_id', '=', $this->adminId];
        if (!empty($param['project_name'])) {
            $where[] = ['project_name', 'like', '%' . $param['project_name'] . '%'];
        }
        if (!empty($param['xkz'])) {
            $where[] = ['xkz', 'like', '%' . $param['xkz'] . '%'];
        }


        $size = $param['size'] ?? '';

        $data = [];

        if (!empty($size)) {
            $list = projectModel::where('status', '>', '0')
                ->where($where)
                ->with('user')
                ->order('id desc')
                ->paginate(10);;
            $data = $list->items();
        } else {
            $list = projectModel::where($where)->with('user')->select();
            $data = $list;
        }



        if (!empty($list)) {
            $paramWhere[] = ['type', 'in', '1,2'];
            $paramMap = parameterModel::selectParameterMap($paramWhere);

            foreach ($data as $item) {
                if (!empty($item->type_id)) {
                    $item->type_name = $paramMap[$item->type_id];
                } else {
                    $item->type_name = '未知';
                }

                if (!empty($item->project_progress_status)) {
                    $item->project_progress_status_name = $paramMap[$item->project_progress_status];
                } else {
                    $item->project_progress_status = '未知';
                }

                if ( !empty($item->user) ) {
                    $item->company_name = $item->user->company_name;
                    unset($item->user);
                } else {
                    $item->company_name = '未知';
                }
            }

        }


        return app('json')->success([
            'list' => $data,
            'total' => empty($size) ? 0 : $list->total(),
            'total_page'=>empty($size) ? 0 : $list->lastPage(),
        ]);

    }


    public function saveProject()
    {

        $param = $this->request->param();
        $param['company_id'] = $this->adminId;
        try {
            validate([
                'project_name|项目名称' => 'require|max:255',
                'project_progress_status|进度' => 'require|number',
//                'xkz|施工许可证' => 'require|max:255',
                'type_id|项目分类' => 'require|number',
                'link_username|项目联系人' => 'require|max:255',
                'link_mobile|项目联系人电话' =>  [
                    'require',
                    'regex:/^1[3456789]\d{9}$/'
                ],
                'province_id|省份' => 'require',
                'province_name|省份' => 'require',
                'city_id|城市' => 'require',
                'city_name|城市' => 'require',
                'area_id|区县' => 'require',
                'area_name|区县' => 'require',
                'address|项目地址' => 'require|max:255',
                'project_progress_status|项目进度'=>'require|number',
                'build_name|施工单位'=>'max:255',
                'build_username|技术负责人'=>'max:255',
                'build_mobile|技术负责人电话'=>['regex:/^1[3456789]\d{9}$/'],
                'construct_name|建设单位'=>'max:255',
                'construct_username|建设单位负责人'=>'max:255',
                'construct_mobile|建设单位负责人电话'=>['regex:/^1[3456789]\d{9}$/'],
                'supervisor_name|监理单位'=>'max:255',
                'supervisor_username|监理单位负责人'=>'max:255',
                'supervisor_mobile|监理单位负责人电话'=>['regex:/^1[3456789]\d{9}$/'],
                'survey_name|勘察单位'=>'max:255',
                'survey_username|勘察单位负责人'=>'max:255',
                'survey_mobile|勘察单位负责人电话'=>['regex:/^1[3456789]\d{9}$/'],
                'design_name|设计单位'=>'max:255',
                'design_username|设计单位联系人'=>'max:255',
                'design_mobile|设计单位联系人电话'=>['regex:/^1[3456789]\d{9}$/'],
                'lng|经度'=>'require|float',
                'lat|纬度'=>'require|float',


            ])->check($param);
            $time = time();
            $projectModel = new projectModel();

            unset($param['create_time'] , $param['update_time']) ;

            if (empty($param['id'])) {
                $param['create_time'] = $time;
                $param['creator_id'] = $this->adminId;
                $projectModel->add($param);
            } else {
                unset($param['create_time']);
                $param['update_time'] = $time;
                $projectModel->edit($param);
            }
            return app('json')->success('操作成功');
        } catch (ValidateException $exception) {
            return app('json')->fail($exception->getMessage());
        }

    }


    public function info()
    {
        $id = $this->request->param('id');
        $projectModel = new projectModel();
        $data = $projectModel->where('id', $id)->where('company_id', $this->adminId)->find();

        if (!empty($data)) {
            $type = parameterModel::getOne($data['type_id']);
            if (!empty($type)) {
                $data['type_name'] = $type['para_name'];
            }
            $progress = parameterModel::getOne($data['project_progress_status']);
            if (!empty($progress)) {
                $data['project_progress_status_name'] = $progress['para_name'];
            }
        }

        return app('json')->success([
            'info' => $data
        ]);
    }



}